package com.canyou.system;

import com.canyou.api.BaseApiController;
import com.canyou.api.ResponseBO;
import com.canyou.constants.CommonConstants;
import com.canyou.constants.MsgConstants;
import com.canyou.system.entity.menu.MenuDTO;
import com.canyou.system.entity.menu.MenuDetailVO;
import com.canyou.system.entity.menu.MenuListVO;
import com.canyou.system.model.Menu;
import com.canyou.system.service.MenuService;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

import static com.canyou.api.ResponseBO.*;

/**
 * 菜单管理控制器
 *
 * @author canyou
 * @since 2019-05-14
 */
@Api(tags = "菜单管理")
@ApiSort(1005)
@RestController
@RequestMapping("/menu")
@Validated
public class MenuController extends BaseApiController {

    @Autowired
    private MenuService menuService;

    @ApiOperation(value = "获取菜单树")
    @ApiImplicitParam(name = "type", paramType = "query", value = "菜单类型，0：菜单，1：按钮",
            dataType = "int", example = "1")
    @PreAuthorize("hasPerm('menu')")
    @GetMapping("/get_list")
    public ResponseBO<List<MenuListVO>> getMenuTree(@RequestParam(required = false) Integer type) {
        List<MenuListVO> menuTree = menuService.getMenuTree(type);
        return ok(menuTree);
    }

    @ApiOperation(value = "获取菜单详情")
    @ApiImplicitParam(name = "id", paramType = "query", value = "菜单id", dataType = "int", required = true, example = "1")
    @PreAuthorize("hasPerm('menu')")
    @GetMapping("/get_info")
    public ResponseBO<MenuDetailVO> getMenu(@NotNull(message = "菜单id不能为空")
                                            @RequestParam(required = false) Long id) {
        return ok(menuService.getDetailById(id));
    }

    @ApiOperation(value = "新增编辑菜单")
    @PreAuthorize("hasPerm('menu:add','menu:edit')  ")
    @PostMapping("/save")
    public ResponseBO saveMenu(@ApiParam(name = "menuDTO", type = "MenuDTO", value = "菜单新增编辑数据", required = true)
                               @Valid
                               @NotNull(message = "机构信息不能为空")
                               @RequestBody MenuDTO menuDTO) {
        Long menuId = menuDTO.getId();
        Long parentId = menuDTO.getParentId();
        // 父部菜单不能为null
        if (!CommonConstants.DEFAULT_PARENT_ID.equals(parentId)) {
            Menu parentMenu = menuService.getById(parentId);
            if (parentMenu == null) {
                return illegal("parentId: " + parentId + "对应的父菜单不存在");
            }
        }
        if (menuService.existMenu(menuDTO.getPermission(), menuId)) {
            return illegal("授权标识已存在");
        }
        Menu menu = new Menu();
        if (menuId != null) {
            menu = menuService.getById(menuId);
            if (menu == null) {
                return illegal("menuId: " + menuId + "对应的菜单不存在");
            }
            if (menuId.equals(parentId)) {
                return illegal("父菜单不能是本菜单");
            }
        }
        BeanUtils.copyProperties(menuDTO, menu);
        return result(menuService.saveOrUpdate(menu), menuId);
    }

    @ApiOperation(value = "删除菜单")
    @PreAuthorize("hasPerm('menu:del')")
    @PostMapping("/remove")
    public ResponseBO removeMenu(@ApiParam(name = "id", type = "Long", value = "菜单id", required = true, example = "1")
                                 @Valid
                                 @NotNull(message = "菜单id不能为空")
                                 @RequestBody Long id) {
        if (menuService.getMenuRoleCount(id) > 0) {
            return illegal("菜单已分配角色, 无法删除");
        }
        boolean res = menuService.removeById(id);
        if (!res) {
            return illegal("删除失败");
        }
        return ok(MsgConstants.DEL_SUCCESS);
    }

}
